/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.webrtc;

import com.workcell.workdata.im.annotation.ClientOrigin;
import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.component.cim.pusher.WebrtcMessagePusher;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.entity.EventMessage;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.model.app.Origin;
import com.workcell.workdata.im.model.request.WebrtcRequest;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.service.RealtimeCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RestController
@RequestMapping("/webrtc")
@Api(produces = "application/json", tags = "单人通话事件推送接口" )
public class WebrtcController {

	@Resource
	private RealtimeCallService realtimeCallService;

	@Resource
	private WebrtcMessagePusher webrtcMessagePusher;

	@ApiOperation(httpMethod = "POST", value = "发起单人语音通话")
	@ApiImplicitParam(name = "targetId", value = "对方用户ID", paramType = "query",  dataTypeClass = Long.class)
	@ApiResponses(value = {@ApiResponse(responseCode = "200" ,description = "OK"),@ApiResponse(responseCode = "403" ,description = "相互不是好友或没在同组织")})
	@PostMapping(value = {"/voice"})
	public ResponseEntity<Void> voice(@ApiIgnore @UID Long uid,
                                      @ApiIgnore @ClientOrigin Origin origin ,
                                      @RequestParam Long targetId) {

		if (!realtimeCallService.isCallable(uid,targetId)){
			return ResponseEntity.make(HttpStatus.FORBIDDEN,"相互不是好友或没在同组织");
		}

		Message message = new EventMessage();
		message.setAction(MessageAction.ACTION_900);
		message.setSender(uid);
		message.setReceiver(targetId);
		webrtcMessagePusher.push(message,origin);

		return ResponseEntity.ok();
	}


	@ApiOperation(httpMethod = "POST", value = "发起单人视频通话")
	@ApiImplicitParam(name = "targetId", value = "对方用户ID", paramType = "query",  dataTypeClass = Long.class)
	@ApiResponses(value = {@ApiResponse(responseCode = "200" ,description = "OK"),@ApiResponse(responseCode = "403" ,description = "相互不是好友")})
	@PostMapping(value =  {"/video"})
	public ResponseEntity<Void> video(@ApiIgnore @UID Long uid,
									  @ApiIgnore @ClientOrigin Origin origin ,
									  @RequestParam Long targetId) {
		if (!realtimeCallService.isCallable(uid,targetId)){
			return ResponseEntity.make(HttpStatus.FORBIDDEN,"相互不是好友或没在同组织");
		}

		Message message = new EventMessage();
		message.setAction(MessageAction.ACTION_901);
		message.setSender(uid);
		message.setReceiver(targetId);
		webrtcMessagePusher.push(message,origin);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "接受通话")
	@ApiImplicitParam(name = "targetId", value = "对方用户ID", paramType = "query",  dataTypeClass = Long.class)
	@PostMapping(value =  {"/accept"})
	public ResponseEntity<Void> accept(@ApiIgnore @UID Long uid,
									   @ApiIgnore @ClientOrigin Origin origin ,
									   @RequestParam Long targetId) {
		Message message = new EventMessage();
		message.setAction(MessageAction.ACTION_902);
		message.setSender(uid);
		message.setReceiver(targetId);
		webrtcMessagePusher.push(message,origin);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "拒绝通话")
	@ApiImplicitParam(name = "targetId", value = "对方用户ID", paramType = "query",  dataTypeClass = Long.class)
	@PostMapping(value =  {"/reject"})
	public ResponseEntity<Void> reject(@ApiIgnore @UID Long uid,
									   @ApiIgnore @ClientOrigin Origin origin ,
									   @RequestParam Long targetId) {

		Message message = new EventMessage();
		message.setAction(MessageAction.ACTION_903);
		message.setSender(uid);
		message.setReceiver(targetId);
		webrtcMessagePusher.push(message,origin);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "反馈正忙")
	@ApiImplicitParam(name = "targetId", value = "对方用户ID", paramType = "query",  dataTypeClass = Long.class)
	@PostMapping(value =  {"/busy"})
	public ResponseEntity<Void> busy(@ApiIgnore @UID Long uid,
									 @ApiIgnore @ClientOrigin Origin origin ,
									 @RequestParam Long targetId) {

		Message message = new EventMessage();
		message.setAction(MessageAction.ACTION_904);
		message.setSender(uid);
		message.setReceiver(targetId);
		webrtcMessagePusher.push(message,origin);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "挂断通话")
	@ApiImplicitParam(name = "targetId", value = "对方用户ID", paramType = "query",  dataTypeClass = Long.class)
	@PostMapping(value =  {"/hangup"})
	public ResponseEntity<Void> hangup(@ApiIgnore @UID Long uid,
									   @ApiIgnore @ClientOrigin Origin origin ,
									   @RequestParam Long targetId) {

		Message message = new EventMessage();
		message.setAction(MessageAction.ACTION_905);
		message.setSender(uid);
		message.setReceiver(targetId);
		webrtcMessagePusher.push(message,origin);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "取消呼叫")
	@ApiImplicitParam(name = "targetId", value = "对方用户ID", paramType = "query",  dataTypeClass = Long.class)
	@PostMapping(value =  {"/cancel"})
	public ResponseEntity<Void> cancel(@ApiIgnore @UID Long uid,
									   @ApiIgnore @ClientOrigin Origin origin ,
									   @RequestParam Long targetId) {

		Message message = new EventMessage();
		message.setAction(MessageAction.ACTION_906);
		message.setSender(uid);
		message.setReceiver(targetId);
		webrtcMessagePusher.push(message,origin);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "同步IceCandidate")
	@PostMapping(value = {"/transmit/ice"})
	public ResponseEntity<Void> ice(@ApiIgnore @UID Long uid,
									@ApiIgnore @ClientOrigin Origin origin ,
									@RequestBody WebrtcRequest request
	) {

		Message message = new EventMessage();
		message.setAction(MessageAction.ACTION_907);
		message.setSender(uid);
		message.setContent(request.getContent());
		message.setReceiver(request.getUid());
		webrtcMessagePusher.push(message,origin);
		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "同步offer")
	@PostMapping(value =  {"/transmit/offer"})
	public ResponseEntity<Void> offer(@ApiIgnore @UID Long uid,
									  @ApiIgnore @ClientOrigin Origin origin ,
									  @RequestBody WebrtcRequest request
	) {

		Message message = new EventMessage();
		message.setAction(MessageAction.ACTION_908);
		message.setSender(uid);
		message.setContent(request.getContent());
		message.setReceiver(request.getUid());
		webrtcMessagePusher.push(message,origin);
		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "同步answer")
	@PostMapping(value =  {"/transmit/answer"})
	public ResponseEntity<Void> answer(@ApiIgnore @UID Long uid,
									   @ApiIgnore @ClientOrigin Origin origin ,
									   @RequestBody WebrtcRequest request
	) {

		Message message = new EventMessage();
		message.setAction(MessageAction.ACTION_909);
		message.setSender(uid);
		message.setContent(request.getContent());
		message.setReceiver(request.getUid());
		webrtcMessagePusher.push(message,origin);
		return ResponseEntity.ok();
	}
}
